package com.woniuxy.controller;

import com.sun.org.apache.xpath.internal.SourceTree;
import com.woniuxy.domain.DataGridView;
import com.woniuxy.domain.ResultDto;
import com.woniuxy.pojo.SysPermission;
import com.woniuxy.service.PermissionService;
import com.woniuxy.service.RoleService;
import com.woniuxy.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/sys")
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;
    @RequestMapping(value = "/toRoleManager.html")
    public String toUserManager(){
        return "/sys/roleList";
    }
    @RequestMapping(value = "/queryAllRole.html")
    @ResponseBody
    public DataGridView queryAllRole(RoleVo roleVo){
        return roleService.getRoleListByPage(roleVo);
    }
    @RequestMapping(value = "/perMenus.html")
    @ResponseBody
    public List<SysPermission> perMenus(){
        System.out.println("****************************");
        return permissionService.getAllMenus();
    }
    @RequestMapping(value = "/getCheckedMenu.html")
    @ResponseBody
    public Integer[] getCheckedMenu(Integer rid){
        return this.roleService.getMenuIdsByRid(rid);
    }
    @RequestMapping(value = "/toAssignRight.html")
    public String toAssignRight(){
        return "/sys/assignRight";
    }
    @RequestMapping("doAssignPermission.html")
    @ResponseBody
    public ResultDto doAssignPermission(Integer rid, String menuIdsStr){
        ResultDto dto=new ResultDto();
        if(menuIdsStr!=null&&menuIdsStr.length()>0){
            String[] menuIdArray=menuIdsStr.split(",");
            Integer[] menuIds=new Integer[menuIdArray.length];
            for (int i=0;i<menuIdArray.length;i++) {
                menuIds[i]=Integer.parseInt(menuIdArray[i]);
            }
            boolean flag=this.roleService.doAssignMenuByRoleId(rid,menuIds);
            if(flag){
                dto.setCode(200);
            }else{
               dto.setCode(-1);
            }
        }
        return dto;
    }
}
